﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eProject.Business;

namespace eProject.Web.UI.Admin
{
    public partial class PlanesManagementEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadData();
                BindSeatType();
                BindSeat();
            }
        }
        private void BindSeatType()
        {
            ddlSeatType.Items.Clear();
            ddlSeatType.ClearSelection();
            ddlSeatType.DataSource = SeatClass.GetAll();
            ddlSeatType.DataBind();
        }
        private void loadData()
        {
            List<Company> company = Company.GetAll();
            ddlCompany.DataSource = company;
            ddlCompany.DataTextField = "Name";
            ddlCompany.DataValueField = "Id";
            ddlCompany.DataBind();
            int id = int.Parse(Request.QueryString["Id"].ToString());
            Planes plane = new Planes(id);
            txtPlaneCode.Text = plane.PlaneId.ToString();
            txtPlaneName.Text = plane.PlaneName.ToString();
            lblCreateDate.Text = plane.CreatedDate.ToShortDateString();
            lblLastUpdate.Text = plane.LastUpdate.ToShortDateString();
        }
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.Load += new EventHandler(this.Page_Load);
            this.btnSubmit.ServerClick += new EventHandler(btnSubmit_Click);
            this.btnReset.ServerClick += new EventHandler(btnReset_Click);
            this.btnInsertSeat.ServerClick += new EventHandler(btnInsertSeat_ServerClick);
            this.grvSeat.RowDeleting += new GridViewDeleteEventHandler(grvSeat_RowDeleting);
        }

        protected void grvSeat_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int Id = int.Parse(grvSeat.DataKeys[e.RowIndex].Value.ToString());
                SeatOfPlanes.Delete(Id);
                BindSeat();
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        private void BindSeat()
        {
            long planeId = int.Parse(Request.QueryString["Id"].ToString());
            grvSeat.DataSource = SeatOfPlanes.GetAll(planeId);
            grvSeat.DataBind();
        }

        public string GetSeatType(object id)
        {
            SeatClass seatclass = new SeatClass(Convert.ToInt32(id));
            return seatclass.SeatName;
        }

        protected void btnInsertSeat_ServerClick(object sender, EventArgs e)
        {
            try
            {
                int SeatId = Convert.ToInt32(ddlSeatType.SelectedValue);
                SeatOfPlanes seat = new SeatOfPlanes();
                int id = int.Parse(Request.QueryString["Id"].ToString());
                seat.PlaneId = id;
                seat.SeatClassId = SeatId;
                seat.TotalSeat = Convert.ToInt32(txtTotalSeat.Text.Trim());
                seat.Save();
                BindSeat();
            }
            catch (System.Exception ex)
            {
                throw ex;

            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                int id = int.Parse(Request.QueryString["Id"].ToString());
                Planes plane = new Planes(id);
                plane.PlaneId = txtPlaneCode.Text.Trim().ToString();
                plane.PlaneName = txtPlaneName.Text.Trim().ToString();
                plane.CompanyId = int.Parse(ddlCompany.SelectedValue.ToString());
                plane.LastUpdate = DateTime.Now;
                plane.Save();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            Response.Redirect("PlanesManagementProgram.aspx");
            //Response.Write(id);
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtPlaneCode.Text = "";
            txtPlaneName.Text = "";
        }
    }
}